package cn.com.server.controller.userManage;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;

import cn.com.server.common.AjaxCodeUtil;
import cn.com.server.common.AjaxObject;
import cn.com.server.entity.Group;
import cn.com.server.entity.User;
import cn.com.server.service.GroupService;
import cn.com.server.service.RoleService;
import cn.com.server.service.UserService;
import cn.com.server.extend.model.RegisterSaveModel;
import cn.com.server.extend.view.UserView;
import cn.com.server.extend.model.UserViewModel;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author qing
 * @since 2017-11-15
 */
@Controller
@RequestMapping("/userManage/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private GroupService groupService;
	
	@RequestMapping("/userList")
	public String userList(HttpServletRequest request, HttpServletResponse response) {
		return "/userManager/userList";
	}
	
	@RequestMapping("/getUserList")
	@ResponseBody
	public AjaxObject getUserList(UserViewModel userModel,HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		Page<UserView> pageData = userService.selectUserViewList(userModel);
		AjaxObject ajaxObject = AjaxCodeUtil.getAjaxObject("success");
		ajaxObject.setData(pageData);
		return ajaxObject;
	}
	
	@RequestMapping("/getUserView")
	@ResponseBody
	public AjaxObject getUserView(Long id,HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		AjaxObject ajaxObject = AjaxCodeUtil.getAjaxObject("success");
		ajaxObject.setData(userService.selectById(id));
		return ajaxObject;
	}
	
	/**
	 * @param registerSaveModel
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public AjaxObject save(RegisterSaveModel registerSaveModel,HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		try {
			if(registerSaveModel.getName()==null||"".equals(registerSaveModel.getName())) {
				return AjaxCodeUtil.getAjaxObject("userNameException");
			}
			
			User user = userService.selectByName(registerSaveModel.getName());
			if(registerSaveModel.getId()!=null) {
				if(user!=null&&!user.getId().equals(registerSaveModel.getId())) {
					return AjaxCodeUtil.getAjaxObject("userExistException");
				}
			}else {
				if(user!=null) {
					return AjaxCodeUtil.getAjaxObject("userExistException");
				}
			}
			if(registerSaveModel.getPassword()==null||"".equals(registerSaveModel.getPassword())) {
				return AjaxCodeUtil.getAjaxObject("passwordException");
			}
			if(registerSaveModel.getRoleId()==null) {
				return AjaxCodeUtil.getAjaxObject("roleIdException");
			}
			Group group = groupService.selectGroupByRoleId(registerSaveModel.getRoleId());
			if(group!=null) {
				if(group.getRegisterFlg()) {
					//终端用户
					if(registerSaveModel.getIp()==null||"".equals(registerSaveModel.getIp())) {
						return AjaxCodeUtil.getAjaxObject("ipException");
					}
					if(registerSaveModel.getId()==null) {
						//添加
						if(userService.insertUserAndTerminalAndUserRole(registerSaveModel)>0) {
							return AjaxCodeUtil.getAjaxObject("success");
						}else {
							return AjaxCodeUtil.getAjaxObject("saveException");
						}
					}else {
						//修改
						if(userService.updateUserAndTerminalAndUserRole(registerSaveModel)>0) {
							return AjaxCodeUtil.getAjaxObject("success");
						}else {
							return AjaxCodeUtil.getAjaxObject("saveException");
						}
					}
				}else {
					//非终端用户
					if(registerSaveModel.getId()==null) {
						//添加
						if(userService.insertUserAndUserRole(registerSaveModel)>0) {
							return AjaxCodeUtil.getAjaxObject("success");
						}else {
							return AjaxCodeUtil.getAjaxObject("saveException");
						}
					}else {
						//修改
						if(userService.updateUserAndUserRole(registerSaveModel)>0) {
							return AjaxCodeUtil.getAjaxObject("success");
						}else {
							return AjaxCodeUtil.getAjaxObject("saveException");
						}
					}
				}
			}else {
				return AjaxCodeUtil.getAjaxObject("groupNotExistException");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxCodeUtil.getAjaxObject("saveException");
		}
	} 
	
	@RequestMapping("/delete")
	@ResponseBody
	public AjaxObject deleteUpdate(String ids,HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		try {
			if(userService.deleteUpdateUserAndUserRoleAndTerminal(ids)>0) {
				return AjaxCodeUtil.getAjaxObject("success");
			}
			return AjaxCodeUtil.getAjaxObject("deleteException");
		}catch (Exception e) {
			e.printStackTrace();
			return AjaxCodeUtil.getAjaxObject("deleteException");
		}
	}
	
	@RequestMapping("/getGroupList")
	@ResponseBody
	public AjaxObject getGroupList(HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		AjaxObject ajaxObject = AjaxCodeUtil.getAjaxObject("success");
		ajaxObject.setData(groupService.selectByRegisterFlg(false));
		return ajaxObject;
	}
	
	@RequestMapping("/getRoleList")
	@ResponseBody
	public AjaxObject getRoleList(Long groupId,HttpServletRequest request, HttpServletResponse response,HttpSession session) {
		AjaxObject ajaxObject = AjaxCodeUtil.getAjaxObject("success");
		ajaxObject.setData(roleService.selectByGroupId(groupId));
		return ajaxObject;
	}
}
